package fr.mywiki.business.navigation;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import fr.mywiki.business.profil.Profile;


/**
 * A <code>SpacesList</code> object represents a list of spaces. It contains a
 * <code>ArrayList</code> of spaces'names and a <code>ArrayList</code> of
 * <code>SpaceFlyWeight</code> objects.
 */
public class SpacesList implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private ArrayList spaces;

	private ArrayList names;

	public SpacesList() {
		spaces = new ArrayList();
		names = new ArrayList();
	}

	/**
	 * Returns an <code>Iterator</code> object along the spaces.
	 * @return an <code>Iterator</code> containing <code>SpaceFlyweight</code> objects
	 */
	public Iterator iterator() {
		return spaces.iterator();
	}
	
	/**
	 * Adds the specified <code>SpaceFlyweight</code> object into the spaces
	 * list.
	 * 
	 * @param spaceFlyweight a <code>SpaceFlyweight</code> object to put in
	 *            the list.
	 */
	public void put(SpaceFlyweight spaceFlyweight) {
		names.add(spaceFlyweight.getName());
		spaces.add(spaceFlyweight);
	}

	/**
	 * This method indicates if the current spaces list contains a space whose
	 * name if the one given in parameter or not.
	 * 
	 * @param name a <code>String</code> object.
	 * @return true if <code>names</code> contains <code>name</code>
	 */
	public boolean containsSpace(String name) {
		return names.contains(name);
	}

	/**
	 * Returns the specified space from the list.
	 * 
	 * @param i the index of the space to return.
	 * @return a <code>SpaceFlyweight</code> object.
	 */
	public SpaceFlyweight getSpace(int i) {
		return (SpaceFlyweight) spaces.get(i);
	}
	
	/**
	 * Returns the specified space name from the list.
	 * 
	 * @param i the index of the name to return.
	 * @return a <code>String</code> object.
	 */
	public String getSpaceName(int i) {
		return (String) names.get(i);
	}
	
	/**
	 * Returns the list of spaces contained in this object.
	 * 
	 * @return a <code>List</code> of <code>SpaceFlyweight</code> objects.
	 */
	public List getSpaces() {
		return spaces;
	}
	
	/**
	 * Returns the spaces list for the given profile.
	 * 
	 * @param id <code>Long</code> identifier of the profile.
	 * @return a SpacesList object.
	 */
	public static SpacesList getSpacesList(Long id) {
		SpacesList spaces = new SpacesList();
		if (Profile.ANONYMOUS_PROFILE_ID.equals(id)) {
			spaces.put(SpaceFlyweight.WIKI_SPACEFLYWEIGHT);
		} else if (Profile.ADMIN_PROFILE_ID.equals(id)) {
			spaces.put(SpaceFlyweight.WIKI_SPACEFLYWEIGHT);
			spaces.put(SpaceFlyweight.FILEMANAGEMENT_SPACEFLYWEIGHT);
      spaces.put(SpaceFlyweight.MODELSMANAGEMENT_SPACEFLYWEIGHT);
			spaces.put(SpaceFlyweight.ADMINISTRATION_SPACEFLYWEIGHT);
		} else if (Profile.AUTHOR_PROFILE_ID.equals(id)) {
			spaces.put(SpaceFlyweight.WIKI_SPACEFLYWEIGHT);
			spaces.put(SpaceFlyweight.FILEMANAGEMENT_SPACEFLYWEIGHT);
		}
		return spaces;
	}
	
	public SpaceFlyweight getSpace(String name) {
		int index = names.indexOf(name);
		if (index < 0)
			return null;
		return (SpaceFlyweight) spaces.get(index);
	}
}
